package com.xinyue.jpa;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.xinyue.jpa.dao.UserDao;
import com.xinyue.jpa.dao.entity.User;
@Service
public class UserService {
	private Logger logger = LoggerFactory.getLogger(UserService.class);
	@Autowired
	private UserDao userDao;
	/**
	 * 保存对象
	 * 
	 * @author 王广帅
	 */
	public void saveUser() {
		User user = new User();
		user.setAge(5);
		user.setCreateTime(new Date());
		user.setName("aaa");
		userDao.save(user);
	}
	/**
	 * 分页查询所有的对象
	 * 
	 * @author 王广帅
	 */
	public void queryUserByPage(){
		Pageable pageable = PageRequest.of(0, 20);
		Page<User> userPage = userDao.findAll(pageable);
		logger.info("total:{}",userPage.getTotalElements());
	}
	/**
	 * 条件分页查询
	 * 
	 * @author 王广帅
	 */
	public void queryUserByAgePage() {
		Pageable pageable = PageRequest.of(0, 20);
		Page<User> userPage = userDao.findByAge(2, pageable);
		logger.info("age and page query total:{}",userPage.getTotalElements());
	}
	
}
